
//#include <interface.hh>
#include <vector>

#include <format_interface.hh>
#include <stdlib.h>
#include <stdio.h>
#include <cpu.hh>
#include <thread>
#include <memory.hh>
#include <disk.hh>
#include <network_load.hh>
#include <vpe_device.hh>
#include <sys/time.h>
#include <vector>
#include <system.hh>
#include <interface.hh>

typedef struct {
     std::vector<format_interface*> monitors;
} system_context;

void* init_system_monitor()
{
    system_context* p_context = new system_context;

    p_context->monitors.push_back(new system_load());
    p_context->monitors.push_back(new cpu_load());
    p_context->monitors.push_back(new disk_load());
    p_context->monitors.push_back(new memory_load());
    p_context->monitors.push_back(new network_load());
    p_context->monitors.push_back(new vpe_device());
    return p_context;
}
char* get_system_status(void* p_context)
{
    system_context* p_sys_context = (system_context*)p_context;
    cJSON* cjson = cJSON_CreateObject();
    std::vector<format_interface*>::iterator it;
    for(it=p_sys_context->monitors.begin();it!=p_sys_context->monitors.end();it++)
    {
        cJSON_AddItemToObject(cjson, (*it)->get_name(), (*it)->to_json());        
    }
    char*  str = cJSON_Print(cjson);     

    cJSON_Delete(cjson);
    return str;
}
void  free_system_status(char* str)
{
    free(str);
}
void close_system_monitor(void* p_context)
{
    system_context* p_sys_context = (system_context*)p_context;
    std::vector<format_interface*>::iterator it;
    for(it=p_sys_context->monitors.begin();it!=p_sys_context->monitors.end();it++)
    {
        delete *it;
        //p_sys_context->monitors.erase(it);       
    }
    p_sys_context->monitors.clear();
    delete p_sys_context;
}